package com.leyou.user.service;

import com.leyou.common.constant.LyConstants;
import com.leyou.common.constant.MQConstants;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    public Boolean checkNameOrPhone(String data, Integer type) {
        User record = new User();
        //根据type的值来判断data的数据类型
        switch (type){
            case 1:
                record.setUsername(data);
                break;
            case 2:
                record.setPhone(data);
                break;
            default:
                throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        //根据条件查询满足的数据的数量
        int count = userMapper.selectCount(record);
        return count!=1;
    }

    public void sendCheckCode(String phone) {
        //生成六位随机数验证码，纯数字
        String code = RandomStringUtils.randomNumeric(6);
        //存入redis中
        redisTemplate.opsForValue().set(LyConstants.CODE_PRE+phone, code, 10, TimeUnit.DAYS);
        //封装一个向MQ发送的数据对象
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);
        //短信验证码放入指定的MQ队列中
        amqpTemplate.convertAndSend(MQConstants.Exchange.SMS_EXCHANGE_NAME,
                MQConstants.RoutingKey.VERIFY_CODE_KEY, map);
    }

    public void register(User user, String code) {
        //获取redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(LyConstants.CODE_PRE + user.getPhone());
        //比对用户输入的验证码是否正确
        if(!StringUtils.equals(redisCode, code)){
            throw new LyException(ExceptionEnum.INVALID_VERIFY_CODE);
        }
        //密码加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //数据入库
        int count = userMapper.insertSelective(user);
        //查看是否成功
        if(count!=1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public User findUserInfo(String username, String password) {
        //根据用户名做查询
        User record = new User();
        record.setUsername(username);
        User user = userMapper.selectOne(record);
        if(user==null){
            throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        //比对密码
        boolean matches = passwordEncoder.matches(password, user.getPassword());
        if(!matches){
            throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
        }
        return user;
    }
}
